/***************************************************************************
 *   Copyright (C) 2007 by Anders Nore									   *
 *   andenore@start.no   												   *
 *                                                                         *
 *   Permission is hereby granted, free of charge, to any person obtaining *
 *   a copy of this software and associated documentation files (the       *
 *   "Software"), to deal in the Software without restriction, including   *
 *   without limitation the rights to use, copy, modify, merge, publish,   *
 *   distribute, sublicense, and/or sell copies of the Software, and to    *
 *   permit persons to whom the Software is furnished to do so, subject to *
 *   the following conditions:                                             *
 *                                                                         *
 *   The above copyright notice and this permission notice shall be        *
 *   included in all copies or substantial portions of the Software.       *
 *                                                                         *
 *   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       *
 *   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    *
 *   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
 *   IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR     *
 *   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
 *   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
 *   OTHER DEALINGS IN THE SOFTWARE.                                       *
 ***************************************************************************/

#include "unp.h"

int Udp_client( const char *hostname, const char *service, SA **saptr, socklen_t *lenp)
{
	struct addrinfo hints, *res, *ressave;
	int sockfd, n;
	
	bzero(&hints, sizeof(struct addrinfo));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	
	if( (n = getaddrinfo(hostname, service, &hints, &res )) != 0) {
		err_quit("Udp_client()::getaddrinfo() %s : %s. Error: %s", hostname, service, gai_strerror(n));
	}
	
	ressave = res;
		
	do {
		if( (sockfd = Socket( res->ai_family, res->ai_socktype, res->ai_protocol)) >= 0)
			break; // success
	} while( (res = res->ai_next) != NULL);

	if(res == NULL)
		err_sys("Udp_client()::Cannot make socket for %s, %s", hostname, service);
	
	*saptr = Malloc(res->ai_addrlen);
	memcpy(*saptr, res->ai_addr, res->ai_addrlen);
	*lenp = res->ai_addrlen;
	
	freeaddrinfo(res);
	
	return sockfd;
}

int Udp_connect( const char *hostname, const char *service)
{
	int sockfd, n;
	struct addrinfo hints, *res, *ressave;	
	
	bzero(&hints, sizeof(struct addrinfo));
	hints.ai_flags = AI_CANONNAME;
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	
	if( (n = getaddrinfo(hostname, service, &hints, &res )) != 0) {
		err_quit("Udp_connect()::getaddrinfo() %s : %s. Error: %s", hostname, service, gai_strerror(n));
	}
	
	ressave = res;
		
	do {
		if( (sockfd = socket( res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
			continue;
		
		if( (connect( sockfd, res->ai_addr, res->ai_addrlen )) == 0 )
			break;
		
		close(sockfd);
	} while ( (res = res->ai_next) != NULL);

	if(res == NULL) 
		err_sys("Udp_connect()::Cannot connect, %s : %s. Error: %s", hostname, service, strerror(errno));		
	freeaddrinfo(ressave);
	
	return sockfd;
}

int Udp_server(char *hostname, char *service, socklen_t *len)
{
	int listenfd, n;
	struct addrinfo hints, *res, *ressave;
	
	bzero(&hints, sizeof(struct addrinfo));
	hints.ai_flags = AI_PASSIVE;
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_DGRAM;
	
	if( (n = getaddrinfo(hostname, service, &hints, &res )) != 0) {
		err_quit("Udp_server()::getaddrinfo() %s : %s. Error: %s", hostname, service, gai_strerror(n));
	}
	
	ressave = res;
		
	do {
		if( (listenfd = socket( res->ai_family, res->ai_socktype, res->ai_protocol)) < 0)
			continue;
		
		if( (bind( listenfd, res->ai_addr, res->ai_addrlen )) == 0 )
			break;
		
		close(listenfd);
		
	} while ( (res = res->ai_next) != NULL);

	if(res == NULL) 
		err_quit("Udp_server()::Cannot connect, %s : %s. Error: %s", hostname, service, strerror(errno));		
	if(len)
		*len = res->ai_addrlen;
	
	freeaddrinfo(ressave);
	
	return listenfd;
}